/**
 * 初始化订单管理详情对话框
 */
var TOrderInfoDlg = {
    tOrderInfoData : {},
    validateFields:{
        consignee: {
            validators: {
                notEmpty: {
                    message: '收货人不能为空'
                }
            }
        },
        receivingAddress: {
            validators: {
                notEmpty: {
                    message: '收货地址不能为空'
                }
            }
        },
        goodsName: {
            validators: {
                notEmpty: {
                    message: '物品名称不能为空'
                }
            }
        },
        receivingPhone: {
            validators: {
                notEmpty: {
                    message: '收货电话不能为空'
                },
                regexp:{
                    regexp: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
                    message: '电话号码格式'
                }
            }
        },
        sendDeptName: {
            validators: {
                notEmpty: {
                    message: '上游网点不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
TOrderInfoDlg.clearData = function() {
    this.tOrderInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOrderInfoDlg.set = function(key, val) {
    this.tOrderInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOrderInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TOrderInfoDlg.close = function() {
    parent.layer.close(window.parent.TOrder.layerIndex);
}

/**
 * 收集数据
 */
TOrderInfoDlg.collectData = function() {
    this
    .set('id')
    .set('orderNum')
    .set('consignee')
    .set('receivingAddress')
    .set('mode',$('#modeType').val())
    .set('goodsName')
    .set('goodsWeight')
    .set('payment')
    .set('backAmount')
    .set('sendDeptName')
    .set('receivingPhone')
    .set('receipt')
    .set('goodsPiece')
    .set('goodsVolume')
    .set('collection')
    .set('province_code',$('#proPlaceName').val())
    .set('city_code',$('#cityPlaceName').val())
    .set('area_code',$('#areaPlaceName').val()).set('mark');

}
/**
 * 验证数据是否为空
 */
TOrderInfoDlg.validate = function () {
    $('#orderInfoForm').data("bootstrapValidator").resetForm();
    $('#orderInfoForm').bootstrapValidator('validate');
    return $("#orderInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
TOrderInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOrder/add", function(data){
        Feng.success("添加成功!");
        window.parent.aTable.refresh();
        TOrderInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOrderInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
TOrderInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOrder/update", function(data){
        Feng.success("修改成功!");
        window.parent.aTable.refresh();
        TOrderInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOrderInfoData);
    ajax.start();
}

$("#proPlaceName").change(function(){
    if($("#proPlaceName").val()!=''){
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/region/religonByPid?id="+$('#proPlaceName').val(), function(data){
            if(data!=''){
                var html="";
                html+="<option  value=''>未选择</option>";
                for(var i=0; i<data.length; i++){
                    html+="<option value='"+data[i].regionId+"'>"+data[i].regionName+"</option>";
                }
                $("#cityPlaceName").html(html);
                $("#areaPlaceName").html("");
            }
        },function(data){
            Feng.error("获取数据失败!" + data.message + "!");
        });
        ajax.start();
    }

})

$("#cityPlaceName").change(function(){
    if($("#cityPlaceName").val()!=''){
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/region/religonByPid?id="+$('#cityPlaceName').val(), function(data){
            if(data!=''){
                var html="";
                html+="<option  value=''>未选择</option>";
                for(var i=0; i<data.length; i++){
                    html+="<option value='"+data[i].regionId+"'>"+data[i].regionName+"</option>";
                }
                $("#areaPlaceName").html(html);
            }
        },function(data){
            Feng.error("获取数据失败!" + data.message + "!");
        });
        ajax.start();
    }

})

$(function() {
    Feng.initValidator("orderInfoForm", TOrderInfoDlg.validateFields);
    $("#sendDeptName").bsSuggest('init', {
        /*url: "/rest/sys/getuserlist?keyword=",
        searchFields: [ "shortAccount"],
        effectiveFieldsAlias:{userName: "姓名"},*/
        effectiveFields: ["id", "dotName"],
        clearable: true,
        url: Feng.ctxPath +"/dot/searchList",
        idField: "id",
        keyField: "dotName"
    }).on('onDataRequestSuccess', function (e, result) {
        console.log('onDataRequestSuccess: ', result);
    }).on('onSetSelectValue', function (e, keyword, data) {
        console.log('onSetSelectValue: ', keyword, data);
    }).on('onUnsetSelectValue', function () {
        console.log('onUnsetSelectValue');
    });




    $("#proPlaceName").val($("#pro_place_name").val());
    $("#cityPlaceName").val($("#city_place_name").val());
    $("#areaPlaceName").val($("#area_place_name").val());

    if($('#is_transfer').val()==1){
        $('#modeType').val(2);
    }else{
        if($('#mode').val()==0){
            $('#modeType').val(0);
        }else{
            $('#modeType').val(1);
        }
    }

});
